package com.example.studentknowledgesys.service.impl;

import com.example.studentknowledgesys.dao.UserMapper;
import com.example.studentknowledgesys.dao.UserProfileMapper;
import com.example.studentknowledgesys.dto.ChangePasswordDTO;
import com.example.studentknowledgesys.entity.User;
import com.example.studentknowledgesys.entity.UserProfile;
import com.example.studentknowledgesys.service.UserProfileService;
import com.example.studentknowledgesys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service( "userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserProfileService userProfileService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public User judgePassword(ChangePasswordDTO changePasswordDTO) {
        User user = userMapper.findByUserId(changePasswordDTO.getUserId());
        if(!matchPassword(changePasswordDTO.getCurrentPassword(), user.getPassword())){
            return null;
        }
        return userMapper.findByUserId(changePasswordDTO.getUserId());
    }



    // 密码加密
    public String encodePassword(String plainPassword) {
        return passwordEncoder.encode(plainPassword);
    }

    // 密码验证
    public boolean matchPassword(String plainPassword, String encodedPassword) {
        return passwordEncoder.matches(plainPassword, encodedPassword);
    }

    // 登录逻辑
    @Override
    public User login(String username, String password) {
        User user = userMapper.findByUsername(username);
        if (user != null && matchPassword(password, user.getPassword())) {
            return user;  // 返回用户信息
        }
        return null;  // 登录失败
    }
    // 注册新用户
    @Override
    @Transactional
    public void register(User user) {
        // 密码加密
        String encodedPassword = encodePassword(user.getPassword());
        user.setPassword(encodedPassword);
        // 插入用户并获取用户id
        userMapper.insertUser(user);
        // 获取插入后的用户id
        Long userId = user.getId();
        UserProfile userProfile = new UserProfile();
        userProfile.setUserId(userId);
        //  创建用户简介
        userProfileService.createUserProfile(userProfile);
//        // 创建用户简介
//        userProfileService.createUserProfile();
    }
    // 修改密码
    @Override
    public void updatePassword(Long id, String password) {
         password = encodePassword(password);
         userMapper.updatePassword(id, password);
    }

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public boolean deleteUser(Long id) {
         return userMapper.deleteUser(id) > 0;
    }

    @Override
    public void updateUserRole(Long userId, String role) {
        userMapper.updateUserRole(userId, role);
    }
}
